const router = require('koa-router')();
const mysqlApi = require('../lib/mysql');
const common = require('../lib/common');
const md5 = require('md5')

//注册页面
router.get('/signup', async (ctx, next) => {
  await ctx.render('signup.ejs', {
    session: ctx.session,
  })
})

//注册
router.post('/signup', async (ctx, next) => {
  let data = ctx.request.body;
  await mysqlApi.seletAdmin_tableUsername(data.username).then(win => {
    if (win.length) {
      ctx.body = {
        code: 1,
        msg: '用户名已经存在'
      }
    } else {
      if (data.password !== data.repeatpassword || data.password == '') {
        ctx.body = {
          code: 2,
          msg: '密码不能为空并且两个密码必须相同'
        }
      } else {
        ctx.body = {
          code: 3,
          msg: '注册成功'
        }
        mysqlApi.insertAdmin_table([data.username, md5(data.password + common.MD5_SUFFIX)])
      }
    }
  })
})

module.exports = router